﻿from database import *

class MapDatabase(DataBase):
    def __init__(self, database_name, header=None, remove=False):
        super(MapDatabase, self).__init__(database_name, remove)
        if header <> None:
            self.insert_header(header)
        
    def insert_header(self, header):
        if self.get_next_id() == '0' and isinstance(header, MapHeader) :
            self.insert(header) 

    def insert_element(self, element):
        if self.get_next_id() <> '0' and isinstance(element, MapElement) :
            self.insert(element)
            
    def insert(self, element):
        super(MapDatabase, self).insert(element)
        
    def get_header(self):
        return super(MapDatabase, self).get_element(0)
        
    def get_element(self, id):
        if id > 0 :
            return super(MapDatabase, self).get_element(id)
        
    def get_elements(self):
        def is_element(x):
            return isinstance(x, MapElement)
            
        elements = super(MapDatabase, self).get_elements()
        return filter(is_element, elements)


class MapHeader(object):
    def __init__(self, **options):
        # imagem
        self.__image_file = None;
        # tamanho da tela (w, h)
        self.__screen_size = (None, None)        
        # tamanho do sprite (w, h)
        self.__sprite_size = (None, None)
        
        self.__set_options(options)
        
    def __set_options(self, options):
        if options.has_key('image_file'): self.__image_file = self.parse(options['image_file'])
        if options.has_key('screen_size'): self.__screen_size = options['screen_size']
        if options.has_key('sprite_size'): self.__sprite_size = options['sprite_size']        
        
    def parse(self, file_name):
        file = file_name[:]
        file = file.split('\\')
        file = file[-1].split('/')
        file = file[-1]
        
        return file
        
    def get_image_file(self): return self.__image_file;
    
    def get_screen_size(self): return self.__screen_size;
    
    def get_sprite_size(self): return self.__sprite_size;

        
class MapElement(object):
    def __init__(self, **options):
        # id
        self.__id = None
        # parte de imagem (x, y)
        self.__subimage = (None, None)
        # posição em uma matriz (i, j)
        self.__position = (None, None)
        # informação
        self.__information = ''
        
        self.__set_options(options)
        
    def __set_options(self, options):
        if options.has_key('id'): self.__id = options['id']
        if options.has_key('subimage'): self.__subimage = options['subimage']
        if options.has_key('position'): self.__position = options['position']
        if options.has_key('information'): self.__information = options['information']
        
    def get_id(self): return self.__id;
    
    def get_subimage(self): return self.__subimage;
    
    def get_position(self): return self.__position;
    
    def get_information(self): return self.__information;        